﻿var add_customerbox;
var edit_customerbox;
var customer_tab;

var add_projectbox;
var edit_projectbox;
var project_tab;


using('messager', function () {
    $("#customer_project_uuid").val("");
    $.messager.progress({
        title: '提示',
        text: '系统加载中，请稍后....'
    });
    page_init();
});

function page_init()
{
    using(['layout','panel', 'dialog','tabs' ,'datagrid','accordion','linkbutton','textbox'], function () {
        $.messager.progress('close');
		$("#Customer_tabs").tabs({
			onSelect:function(title,index){
				if(index==1)
				{
				    setTimeout(function () {
				        var uuid = $("#customer_project_uuid").val();
				        if (uuid == "") {
				            $.messager.alert('提示', "请先选择客户，再先择项目！", 'info');
				            $("#Customer_tabs").tabs("select", 0);
				            return;
				        }
				    }, 100);
					
				}
			}
			});
		
		$("#userinfo").layout({fit:true});
		$("#userinfoquery").textbox(
		{labelPosition:'before',
		label:'请输入要查询的客户名称:',
		labelWidth:200,
		buttonText:"查询",
		buttonAlign:"right",
		buttonIcon:"icon-search",
		onClickButton:function(){
			var querystr=$("#userinfoquery").textbox("getValue");
			$(customer_tab).datagrid({
				queryParams:{customeruser:querystr}
				});
		}
		});
		
		customer_tab=$("#customertab").datagrid(
		{
			title:"客户列表",
			method:"post",
			url:webpath+"Customer/QueryCustomer",
			queryParams:{customeruser:""},
			singleSelect:true,
			rownumbers:true,
			fit:true,
			fitColumns:true,
			pageSize : 10,
			pageList : [ 10,15, 25, 35, 45, 55,65,100],
			nowrap : false,
			pagination:true,
			showFooter:true,
			iconCls:'icon-man',
			columns:[[
				{field:'clientinfo_uuid',hidden:true},
				{field:'client_name',title:'客户名称',width:600,fixed:true}
        	]],toolbar: [
			{
			iconCls: 'icon-ok',
			text:"打开",
			handler: function(){
				open_project();
				}
			},
			{
			iconCls: 'icon-add',
			text:"添加",
			handler: function(){
				$(add_customerbox).dialog("open");
				$("#customerinput").textbox("clear");
				}
			},
			{
			iconCls: 'icon-edit',
			text:"修改",
			handler: function(){
				var none=$(customer_tab).datagrid("getSelected");
				if(none==null)
				{
					$.messager.alert('提示',"请先选择要修改的客户！", 'info');
					return;
				}
				$("#edit_uuid").val(none.clientinfo_uuid);
				$("#oldcustomeredit").textbox("setValue",none.client_name);
				$("#newcustomeredit").textbox("setValue","");
				$(edit_customerbox).dialog("open");
				}
			},
			{
			iconCls: 'icon-remove',
			text:"删除",
			handler: function(){
				var none=$(customer_tab).datagrid("getSelected");
				if(none==null)
				{
					$.messager.alert('提示',"请先选择要删除的客户信息！", 'info');
					return;
				}

					
					$.messager.confirm("提示","确认删除《"+none.client_name+"》吗?",function(r){
						if(r)
						{
							$.messager.progress({title: '提示',text: '正在提交数据，请稍后....'});
							$.post(webpath+"Customer/DelCustomer",
							{"uuid":none.clientinfo_uuid},
							function(data){
								rajax(data);
								if(data.code==200)
								{
									$(customer_tab).datagrid("reload");
								}
							},"json");
						}
					});
				
				}
			},
			{
			iconCls: 'icon-reload',
			text:"刷新",
			handler: function(){$(customer_tab).datagrid("reload");}
			}
			],
			//双击打开工程页面
			onDblClickCell:function(index,field,value)
			{
				open_project();
			}
		}
		);
		
		
		add_customerbox=$("#add_customer").dialog({
			title: '添加新客户',
			closed: true,
			modal: true,
			width: 520,
			height: 150,
			buttons:[{
				text:'保存',
				iconCls:'icon-save',
				handler:function(){
					var customerstr=$("#customerinput").textbox("getValue");
					if(customerstr==null||customerstr=="")
					{
						$.messager.alert('提示','请先输入客户信息，再点击保存','info');
						return;
					}
					
					$.messager.confirm("提示","确认保存该户信息吗?",function(r){
						if(r)
						{
							$.messager.progress({title: '提示',text: '正在提交数据，请稍后....'});
							$.post(webpath+"Customer/AddCustomer",
							{"customer":encodeURIComponent(customerstr)},
							function(data){
								rajax(data);
								if(data.code==200)
								{
									$(add_customerbox).dialog("close");
									$(customer_tab).datagrid("reload");
								}
							},"json");
						}
					});
					}
				},
				{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function(){
					$(add_customerbox).dialog("close");
					}
				}
				]
		});
		
		$("#add_customer").css("visibility","visible");
		
		$("#customerinput").textbox(
		{labelPosition:'before',
		label:'请输入客户名称:',
		labelWidth:140
		});
		
		edit_customerbox=$("#edit_customer").dialog({
			title: '修改客户信息',
			closed: true,
			modal: true,
			width: 520,
			height: 170,
			buttons:[{
				text:'修改',
				iconCls:'icon-save',
				handler:function(){
					var uuid=$("#edit_uuid").val();
					var newcustomeredit=$("#newcustomeredit").textbox("getValue");
					
					if(newcustomeredit==null||newcustomeredit=="")
					{
						$.messager.alert('提示','请先输入客户信息，再点击修改','info');
						return;
					}
					
					$.messager.confirm("提示","确认修改该户信息吗?",function(r){
						if(r)
						{
							$.messager.progress({title: '提示',text: '正在提交数据，请稍后....'});
							$.post(webpath+"Customer/EditCustomer",
							{"customer":encodeURIComponent(newcustomeredit),"uuid":uuid},
							function(data){
								rajax(data);
								if(data.code==200)
								{
									$(edit_customerbox).dialog("close");
									$(customer_tab).datagrid("reload");
								}
							},"json");
						}
					});
					}
				},
				{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function(){
					$(edit_customerbox).dialog("close");
					}
				}
			]
		});
		
		$("#oldcustomeredit").textbox(
		{labelPosition:'before',
		label:'客户名称:',
		labelWidth:80,
		readonly:true
		});
		
		$("#newcustomeredit").textbox(
		{labelPosition:'before',
		label:'修改为:',
		labelWidth:80
		});
		
		$("#edit_customer").css("visibility","visible");
		
		init_project();
    });
	
	
}

//初始化项目
function init_project()
{
	$("#projectinfo").layout({fit:true});
	
	$("#projectinfoquery").textbox(
		{labelPosition:'before',
		label:'请输入要查询的项目名称:',
		labelWidth:200,
		buttonText:"查询",
		buttonAlign:"right",
		buttonIcon:"icon-search",
		onClickButton:function(){
			var querystr=$("#projectinfoquery").textbox("getValue");
			$(project_tab).datagrid({
			    queryParams: {
			        projectname: "",
			        uuid: $("#customer_project_uuid").val()
			    }
				});
		}
		});
	
	var add_projectbox=$("#add_project").dialog({
			title: '添加新项目',
			closed: true,
			modal: true,
			width: 520,
			height: 150,
			buttons:[{
				text:'保存',
				iconCls:'icon-save',
				handler:function(){
				    var projectstr = $("#projectinput").textbox("getValue");
				    if (projectstr == null || projectstr == "")
					{
						$.messager.alert('提示','请先输入客户信息，再点击保存','info');
						return;
					}
					
					$.messager.confirm("提示","确认保存该户信息吗?",function(r){
						if(r)
						{
							$.messager.progress({title: '提示',text: '正在提交数据，请稍后....'});
							$.post(webpath + "Customer/Addproject",
							{
							    "projectstr": encodeURIComponent(projectstr),
							    "uuid": $("#customer_project_uuid").val()
							},
							function(data){
								rajax(data);
								if(data.code==200)
								{
								    $(add_projectbox).dialog("close");
								    $(project_tab).datagrid("reload");
								}
							},"json");
						}
					});
					}
				},
				{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function(){
					$(add_projectbox).dialog("close");
					}
				}
				]
			});
	$("#add_project").css("visibility", "visible");
	
	
	edit_projectbox=$("#edit_project").dialog({
			title: '修改项目信息',
			closed: true,
			modal: true,
			width: 520,
			height: 170,
			buttons:[{
				text:'修改',
				iconCls:'icon-save',
				handler:function(){
					var uuid=$("#project_edit_uuid").val();
					var newprojectedit=$("#newprojectedit").textbox("getValue");
					
					if(newprojectedit==null||newprojectedit=="")
					{
						$.messager.alert('提示','请先输入客户信息，再点击修改','info');
						return;
					}
					
					$.messager.confirm("提示","确认修改该户信息吗?",function(r){
						if(r)
						{
							$.messager.progress({title: '提示',text: '正在提交数据，请稍后....'});
							$.post(webpath+"Customer/EditProject",
							{"projectname":encodeURIComponent(newprojectedit),"uuid":uuid},
							function(data){
								rajax(data);
								if(data.code==200)
								{
									$(edit_projectbox).dialog("close");
									$(project_tab).datagrid("reload");
								}
							},"json");
						}
					});
					}
				},
				{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function(){
					$(edit_customerbox).dialog("close");
					}
				}
			]
		});
		
		
		$("#edit_project").css("visibility","visible");
	
	project_tab=$("#projectab").datagrid(
		{
			method:"post",
			singleSelect:true,
			rownumbers:true,
			fit:true,
			fitColumns:true,
			pageSize : 10,
			pageList : [ 10,15, 25, 35, 45, 55,65,100],
			nowrap : false,
			pagination:true,
			showFooter:true,
			//iconCls:'icon-large-smartart',
			columns:[[
				{field:'proinfo_uuid',hidden:true},
				{field:'pro_name',title:'项目名称',width:600,fixed:true}
        	]],toolbar: [
			{
			iconCls: 'icon-add',
			text:"添加",
			handler: function(){
			    $(add_projectbox).dialog("open");
			    $("#projectinput").textbox("clear");
				}
			},
			{
			iconCls: 'icon-edit',
			text:"修改",
			handler: function(){
				var none=$(project_tab).datagrid("getSelected");
				if(none==null)
				{
					$.messager.alert('提示',"请先选择要修改的项目！", 'info');
					return;
				}
				$("#project_edit_uuid").val(none.proinfo_uuid);
				$("#oldprojectedit").textbox("setValue",none.pro_name);
				$("#newprojectedit").textbox("setValue","");
				$(edit_projectbox).dialog("open");
				}
			},
			{
			iconCls: 'icon-remove',
			text:"删除",
			handler: function(){
				var none=$(project_tab).datagrid("getSelected");
				if(none==null)
				{
					$.messager.alert('提示',"请先选择要删除的项目信息！", 'info');
					return;
				}

					
					$.messager.confirm("提示","确认删除《"+none.pro_name+"》吗?",function(r){
						if(r)
						{
							$.messager.progress({title: '提示',text: '正在提交数据，请稍后....'});
							$.post(webpath+"Customer/DelProject",
							{"uuid":none.proinfo_uuid},
							function(data){
								rajax(data);
								if(data.code==200)
								{
									$(project_tab).datagrid("reload");
								}
							},"json");
						}
					});
				
				}
			},
			{
			iconCls: 'icon-reload',
			text:"刷新",
			handler: function(){$(project_tab).datagrid("reload");}
			}
			]
		}
		);


	$("#projectinput").textbox(
		{
		    labelPosition: 'before',
		    label: '请输入项目名称:',
		    labelWidth: 140
		});


	$("#oldprojectedit").textbox(
		{
		    labelPosition: 'before',
		    label: '项目名称:',
		    labelWidth: 80,
		    readonly: true
		});

	$("#newprojectedit").textbox(
    {
        labelPosition: 'before',
        label: '修改为:',
        labelWidth: 80
    });
}


function open_project()
{
	var none=$(customer_tab).datagrid("getSelected");
	if(none==null)
	{
		$.messager.alert('提示',"请先选择要打开客户名称！", 'info');
		return;
	}
	
	$("#customer_project_uuid").val(none.clientinfo_uuid);
	
	$(project_tab).datagrid({
	    title: ("项目信息—当前客户：《" + none.client_name + "》"),
	    url: webpath + "Customer/QueryProject",
	    queryParams: {
	        projectname: "",
	        uuid: $("#customer_project_uuid").val()
	    }
	});
	
	$("#Customer_tabs").tabs("select",1);
	
	
	
	
}
